﻿// Decompiled with JetBrains decompiler
// Type: MCM.LightInject.ReflectionHelper
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;

#nullable disable
namespace MCM.LightInject
{
  [ExcludeFromCodeCoverage]
  internal static class ReflectionHelper
  {
    private static readonly Lazy<ThreadSafeDictionary<Type, MethodInfo>> GetInstanceWithParametersMethods = ReflectionHelper.CreateLazyGetInstanceWithParametersMethods();

    public static MethodInfo GetGetInstanceWithParametersMethod(Type serviceType)
    {
      // ISSUE: reference to a compiler-generated field
      // ISSUE: reference to a compiler-generated field
      return ReflectionHelper.GetInstanceWithParametersMethods.Value.GetOrAdd(serviceType, ReflectionHelper.\u003C\u003EO.\u003C0\u003E__CreateGetInstanceWithParametersMethod ?? (ReflectionHelper.\u003C\u003EO.\u003C0\u003E__CreateGetInstanceWithParametersMethod = new Func<Type, MethodInfo>(ReflectionHelper.CreateGetInstanceWithParametersMethod)));
    }

    public static Delegate CreateGetNamedInstanceWithParametersDelegate(
      IServiceFactory factory,
      Type delegateType,
      string serviceName)
    {
      Type[] genericTypeArguments = delegateType.GetTypeInfo().GenericTypeArguments;
      return (Delegate) typeof (ReflectionHelper).GetTypeInfo().DeclaredMethods.Single<MethodInfo>((Func<MethodInfo, bool>) (m => m.GetGenericArguments().Length == genericTypeArguments.Length && m.Name == "CreateGenericGetNamedParameterizedInstanceDelegate")).MakeGenericMethod(genericTypeArguments).Invoke((object) null, new object[2]
      {
        (object) factory,
        (object) serviceName
      });
    }

    private static Lazy<ThreadSafeDictionary<Type, MethodInfo>> CreateLazyGetInstanceWithParametersMethods()
    {
      return new Lazy<ThreadSafeDictionary<Type, MethodInfo>>((Func<ThreadSafeDictionary<Type, MethodInfo>>) (() => new ThreadSafeDictionary<Type, MethodInfo>()));
    }

    private static MethodInfo CreateGetInstanceWithParametersMethod(Type serviceType)
    {
      Type[] genericTypeArguments = serviceType.GetTypeInfo().GenericTypeArguments;
      return typeof (ServiceFactoryExtensions).GetTypeInfo().DeclaredMethods.Single<MethodInfo>((Func<MethodInfo, bool>) (m => m.Name == "GetInstance" && m.GetGenericArguments().Length == genericTypeArguments.Length && ((IEnumerable<ParameterInfo>) m.GetParameters()).All<ParameterInfo>((Func<ParameterInfo, bool>) (p => p.Name != "serviceName")))).MakeGenericMethod(genericTypeArguments);
    }

    private static Func<TArg, TService> CreateGenericGetNamedParameterizedInstanceDelegate<TArg, TService>(
      IServiceFactory factory,
      string serviceName)
    {
      return (Func<TArg, TService>) (arg => factory.GetInstance<TArg, TService>(arg, serviceName));
    }

    private static Func<TArg1, TArg2, TService> CreateGenericGetNamedParameterizedInstanceDelegate<TArg1, TArg2, TService>(
      IServiceFactory factory,
      string serviceName)
    {
      return (Func<TArg1, TArg2, TService>) ((arg1, arg2) => factory.GetInstance<TArg1, TArg2, TService>(arg1, arg2, serviceName));
    }

    private static Func<TArg1, TArg2, TArg3, TService> CreateGenericGetNamedParameterizedInstanceDelegate<TArg1, TArg2, TArg3, TService>(
      IServiceFactory factory,
      string serviceName)
    {
      return (Func<TArg1, TArg2, TArg3, TService>) ((arg1, arg2, arg3) => factory.GetInstance<TArg1, TArg2, TArg3, TService>(arg1, arg2, arg3, serviceName));
    }

    private static Func<TArg1, TArg2, TArg3, TArg4, TService> CreateGenericGetNamedParameterizedInstanceDelegate<TArg1, TArg2, TArg3, TArg4, TService>(
      IServiceFactory factory,
      string serviceName)
    {
      return (Func<TArg1, TArg2, TArg3, TArg4, TService>) ((arg1, arg2, arg3, arg4) => factory.GetInstance<TArg1, TArg2, TArg3, TArg4, TService>(arg1, arg2, arg3, arg4, serviceName));
    }
  }
}
